package com.fang.networkdisk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fang.common.common.CommonPage;
import com.fang.common.exception.ExceptionCode;
import com.fang.common.exception.NetworkDiskException;
import com.fang.networkdisk.entity.bo.RoleBo;
import com.fang.networkdisk.entity.po.TbNetDisRole;
import com.fang.networkdisk.entity.vo.RoleVo;
import com.fang.networkdisk.mapper.TbNetDisRoleMapper;
import com.fang.networkdisk.service.TbNetDisRoleService;
import com.fang.networkdisk.util.NDBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author Bernie_fang
 * @since 2024-12-07
 */
@Service
public class TbNetDisRoleServiceImpl extends ServiceImpl<TbNetDisRoleMapper, TbNetDisRole> implements TbNetDisRoleService {

    @Resource
    private TbNetDisRoleMapper tbNetDisRoleMapper;

    @Override
    public CommonPage queryRole(RoleBo roleBo) {
//        QueryWrapper<TbNetDisRole> wrapper = new QueryWrapper<>();
//        wrapper.eq(IntegerUtils.isNotEmpty(roleBo.getId()), "id", roleBo.getId())
//                .likeRight(StringUtils.isNotEmpty(roleBo.getIdentifier()), "identifier", roleBo.getIdentifier())
//                .likeRight(StringUtils.isNotEmpty(roleBo.getName()), "name", roleBo.getName())
//                .likeRight(StringUtils.isNotEmpty(roleBo.getDescription()), "description", roleBo.getDescription())
//                .eq(StringUtils.isNotEmpty(roleBo.getType()), "type", roleBo.getType())
//                .eq(StringUtils.isNotEmpty(roleBo.getLevel()), "level", roleBo.getLevel())
//                .likeRight(StringUtils.isNotEmpty(roleBo.getDepartment()), "department", roleBo.getDepartment())
//                .eq(StringUtils.isNotEmpty(roleBo.getCreateUser()), "create_user", roleBo.getCreateUser())
//                .eq(StringUtils.isNotEmpty(roleBo.getUpdateUser()), "update_user", roleBo.getUpdateUser())
//                .apply(DateUtils.isNotEmpty(roleBo.getCreatedTime()), "date_format(created_time, '%Y-%m-%d') = {0}", DateUtils.getDateString(roleBo.getCreatedTime()));
//        Page<TbNetDisRole> page = new Page<>(roleBo.getCurPage(), roleBo.getPageSize());
//        this.tbNetDisRoleMapper.selectPage(page, wrapper);
//        List<PermissionVo> resultList = NDBeanUtils.copyListProperties(page.getRecords(), PermissionVo.class);
//        CommonPage result = new CommonPage(roleBo.getCurPage(), page.getTotal(), resultList);
        roleBo.setOffset((roleBo.getCurPage() - 1) * roleBo.getPageSize());
        List<RoleVo> requestPathVos = this.tbNetDisRoleMapper.queryRole(roleBo);
        int total = this.tbNetDisRoleMapper.queryRoleCount(roleBo);
        CommonPage result = new CommonPage(roleBo.getCurPage(), total, requestPathVos);
        return result;
    }

    @Override
    public TbNetDisRole findRoleByIdentifier(String identifier) {
        QueryWrapper<TbNetDisRole> wrapper = new QueryWrapper<>();
        wrapper.eq("identifier", identifier);
        TbNetDisRole tbNetDisRole = this.tbNetDisRoleMapper.selectOne(wrapper);
        return tbNetDisRole;
    }

    @Override
    public int addRole(RoleBo roleBo) {
        TbNetDisRole db_Role = findRoleByIdentifier(roleBo.getIdentifier());
        if (NDBeanUtils.isObjectNotEmpty(db_Role)) {
            throw new NetworkDiskException(ExceptionCode.DATA_DUPLICATE);
        }
        TbNetDisRole role = new TbNetDisRole();
        NDBeanUtils.copyListProperties(roleBo, role);
        return tbNetDisRoleMapper.insert(role);
    }

    @Override
    public int updateRoleById(RoleBo roleBo) {
        TbNetDisRole role = new TbNetDisRole();
        NDBeanUtils.copyListProperties(roleBo, role);
        return tbNetDisRoleMapper.updateById(role);
    }

    @Override
    public int deleteRoleById(Integer id, String authorId) {
        UpdateWrapper<TbNetDisRole> wrapper = new UpdateWrapper<>();
        wrapper.set("update_user", authorId)
                .set("updated_time", new Date())
                .set("deleted", true)
                .eq("id", id);
        return tbNetDisRoleMapper.update(null, wrapper);
    }

}
